package com.sjj.shopping.webSocket;

import com.alibaba.fastjson.JSON;
import javax.websocket.OnClose;

import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.stereotype.Component;

import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * className:webSocketGame1
 * Package:com.sjj.shopping.webSocket
 * Description:
 *
 * @Date: 2022/8/16 19:43
 * @Auther: wssky19890629@163.com
 */

@ServerEndpoint("/websocket/game")
@Component
public class webSocketGame1 {
    //贪吃蛇思路
    //前端进入页面，建立websocket连接
    //设置一个变量i代表方向，每次前端发消息会改变i
    //open后触发一个线程任务，该线程任务轮询一个A操作，每执行一次，休眠1秒钟
    //A操作：根据i值更新一个二维数组，然后把二维数组发送给前端
    private static CopyOnWriteArraySet<webSocketGame1> webSocketGame1Set= new CopyOnWriteArraySet<>();
    private Session session;
    Random r=new Random();
    List<Integer[]> body=new ArrayList<Integer[]>(); //身体
//    private String userId="";
    static int foodHang=10;
    static int foodLie=10;
    int fangxiang=2;

    @OnClose
    public void onClose(){
        webSocketGame1Set.remove(this);
    }

    @OnOpen
    public void onOpen(Session session) throws InterruptedException, IOException {


        this.session=session;
//        this.userId=userId;
        webSocketGame1Set.add(this);

        //初始化蛇
        Integer[] start=new Integer[2];
        Integer[] start1=new Integer[2];
        start[0]=0;
        start[1]=1;
        start1[0]=0;
        start1[1]=0;
        body.add(start);  //初始化
        body.add(start1);

        new Thread(()->{
            //标志位
            int flag=1;
            while(flag==1){
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    flag=move();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    //向客户端发消息：所有蛇的坐标+食物坐标
                    List<Integer[]> allBody=new ArrayList<Integer[]>();
                    for(webSocketGame1 game:webSocketGame1Set){
                       for(Integer[] zuobiao:game.body){
                           allBody.add(zuobiao);
                       }
                    }
                    this.sendMessage(JSON.toJSONString(allBody)+"."+foodHang+"."+foodLie);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public  void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }


    @OnMessage
    public void onMessage(String kk){
        //收到游览器消息，改变方向
        fangxiang=Integer.parseInt(kk);
    }

    public int move() throws IOException {
        //获得队头的坐标
        int hang=body.get(0)[0];
        int lie=body.get(0)[1];

        if(fangxiang==1){
            hang--;
        } else if(fangxiang==2){
            lie++;
        } else if(fangxiang==3){
            hang++;
        } else{
            lie--;
        }

        //撞到边界,断开连接,结束线程
        if(hang>18||hang<0||lie>18||lie<0){
            this.onClose();
            this.session.close();
            return  0;
        }

        for(webSocketGame1 webSocketGame:webSocketGame1Set){
            for(Integer[] zuobiao:webSocketGame.body){
                if(hang==zuobiao[0]&&lie==zuobiao[1]){
                    this.onClose();
                    this.session.close();
                    return  0;
                }
            }
        }

        if(hang==foodHang&&lie==foodLie){  //吃到球了
            foodHang=r.nextInt(19);
            foodLie=r.nextInt(19);
            Integer[] zuobiao=new Integer[2];
            zuobiao[0]=hang;
            zuobiao[1]=lie;
            body.add(0,zuobiao); //队头插入
            return 1; //跳过删除队尾操作
        }


        Integer[] zuobiao=new Integer[2];
        zuobiao[0]=hang;
        zuobiao[1]=lie;

        body.add(0,zuobiao); //队头插入
        body.remove(body.size()-1); //删除队尾
        return 1;
    }
}
